package com.itheima.controller;



import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户名展示
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getUsername")
    public Result getusername() {
        //安全框架认证以后，可以将信息存到框架提供的上下文对象中
        //SecurityContextHolder.getContext():获取上下文
        //SecurityContextHolder.getContext().getAuthentication():获取认证信息
        //SecurityContextHolder.getContext().getAuthentication().getPrincipal():获取user对象(框架提供的user)
        org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user != null) {
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user.getUsername());
        }
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    @Reference
    private UserService userService;

    //   新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.add(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //如果新增失败
            return new Result(false, MessageConstant.ADD_USER_FAIL);
        }
//        新增成功
        return new Result(true, MessageConstant.ADD_USER_SUCCESS);
    }

    //    分页查询
    @RequestMapping("/findpage")
    public PageResult findpage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = userService.pageQuery(queryPageBean);
        return pageResult;
    }

    //回显数据
    @RequestMapping("/copyback")
    public Result copyback(String username) {
        try {
            User user = userService.copyback(username);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //回显数据中角色信息
    @RequestMapping("/copybackids")
    public Result copybackids(Integer id) {
        try {
            List<Integer> integerList = userService.copybackids(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, integerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //   编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            userService.edit(user, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            //如果编辑失败
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
//        编辑成功
        return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
    }

    //删除
    @RequestMapping("/del")
    public Result del(Integer id) {
        try {
            userService.del(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
    }

    @RequestMapping("/FindAll")
    public Result find() {
        List<User> userList = userService.findalluser();
        if (userList != null && userList.size() > 0) {
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, userList);
        }
        return new Result(false, MessageConstant.QUERY_USER_FAIL);
    }

}
